<template>
    <div class="sidebar">
        <el-menu
            class="sidebar-el-menu"
            :default-active="onRoutes"
            :collapse="collapse"
            background-color="#324157"
            text-color="#bfcbd9"
            active-text-color="#20a0ff"
            unique-opened
            router
        >
            <template v-for="item in items">
				<template  v-if="role>=item.role">
					<template v-if="item.child">
						<el-submenu :index="item.index" :key="item.index">
							<template slot="title">
								<i :class="item.icon"></i>
								<span slot="title">{{ item.name }}</span>
							</template>
							<template v-for="subItem in item.child">
								<el-submenu
									v-if="subItem.child"
									:index="subItem.index"
									:key="subItem.index"
								>
									<template slot="title">{{ subItem.title }}</template>  
								</el-submenu>
								<el-menu-item
									v-else
									:index="subItem.index"
									:key="subItem.index"
								>
									<i class="el-icon-arrow-right"></i>
									<span slot="title">{{ subItem.name }}</span>
								</el-menu-item>
							</template>
						</el-submenu>
					</template>
					<template v-else>
						<el-menu-item :index="item.index" :key="item.index">
							<i :class="item.icon"></i>
							<span slot="title">{{ item.name }}</span>
						</el-menu-item>
					</template>
				</template>
			</template>
        </el-menu>
    </div>
</template>

<script>
import { fetchData } from '../../api/https'
import bus from '../common/bus'
export default {
    data() {
        return {
            collapse: false,
            items: [],
			path: '',
			role:0
        };
    },
    computed: {
        onRoutes() {
			let p = this.$route.path
            return p
        }
    },
    created() {
		this.path = this.$route.path
        // 通过 Event Bus 进行组件间通信，来折叠侧边栏
        bus.$on('collapse', msg => {
            this.collapse = msg;
            bus.$emit('collapse-content', msg);
        });
        this.getMenu()
    },
    methods: {
        getMenu () {
            let self = this
            this.$apiPost('index/getMenu').then(res => {
                self.items = res.data  
				self.role = localStorage.getItem('role')
            })
        }
    }
};
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
	overflow: auto;
	
}
.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
    width: 180px;
}
.sidebar > ul {
    height: 100%;
}
</style>
